import gps from "./gps";
import utils from "./utils";

export function formatterStateStationRealtime(data1, data2) {
  if (!data1.success || !data2.success) {
    return [];
  }
  let array1 = [].concat.apply([], data1.data);
  let array2 = Array.from(data2.data);
  let array3 = array1.filter(item1 => {
    return array2.some(item2 => {
      if (item1.station_code === item2.deviceId) {
        let wgs = gps.bd09ToWgs84(item2.lat, item2.lng);
        item1.longitude = wgs.lng;
        item1.latitude = wgs.lat;
        return true;
      }
    });
  });
  return array3.sort(utils.compare(["aqi"], 0));
}

export function formatterWeather(data1, data2) {
  let weather = {
    cityName: "",
    rank: "—",
    percent: "—",
    aqi: "—",
    co: "—",
    no2: "—",
    o3: "—",
    pm10: "—",
    pm25: "—",
    so2: "—",
    first: "—",
    condition: "—",
    temp: "—",
    humidity: "—",
    windLevel: "—"
  };
  if (data1.code !== 0 || data2.code !== 0) {
    return weather;
  }
  weather.cityName = data1.data.aqi.cityName ? data1.data.aqi.cityName : "—";
  if (data1.data.aqi.rank) {
    let rank = data1.data.aqi.rank.split("/");
    if (rank.length === 2) {
      weather.rank = rank[0];
      weather.percent = Math.round(((rank[1] - rank[0]) / rank[1]) * 100) + "%";
    }
  }
  weather.aqi = data1.data.aqi.value ? data1.data.aqi.value - 0 : "—";
  weather.co = data1.data.aqi.coC ? data1.data.aqi.coC - 0 : "—";
  weather.no2 = data1.data.aqi.no2C ? data1.data.aqi.no2C - 0 : "—";
  weather.o3 = data1.data.aqi.o3C ? data1.data.aqi.o3C - 0 : "—";
  weather.pm10 = data1.data.aqi.pm10C ? data1.data.aqi.pm10C - 0 : "—";
  weather.pm25 = data1.data.aqi.pm25C ? data1.data.aqi.pm25C - 0 : "—";
  weather.so2 = data1.data.aqi.so2C ? data1.data.aqi.so2C - 0 : "—";
  if (weather.aqi > 50) {
    if (
      utils.validateIsNumber(data1.data.aqi.co) &&
      data1.data.aqi.co - 0 >= weather.aqi
    ) {
      weather.first = "CO";
    } else if (
      utils.validateIsNumber(data1.data.aqi.no2) &&
      data1.data.aqi.no2 - 0 >= weather.aqi
    ) {
      weather.first = "NO2";
    } else if (
      utils.validateIsNumber(data1.data.aqi.o3) &&
      data1.data.aqi.o3 - 0 >= weather.aqi
    ) {
      weather.first = "O3";
    } else if (
      utils.validateIsNumber(data1.data.aqi.pm10) &&
      data1.data.aqi.pm10 - 0 >= weather.aqi
    ) {
      weather.first = "PM10";
    } else if (
      utils.validateIsNumber(data1.data.aqi.pm25) &&
      data1.data.aqi.pm25 - 0 >= weather.aqi
    ) {
      weather.first = "PM2.5";
    } else if (
      utils.validateIsNumber(data1.data.aqi.so2) &&
      data1.data.aqi.so2 - 0 >= weather.aqi
    ) {
      weather.first = "SO2";
    }
  }
  weather.condition = data2.data.condition.condition
    ? data2.data.condition.condition
    : "—";
  weather.temp = data2.data.condition.temp
    ? data2.data.condition.temp + "℃"
    : "—";
  weather.humidity = data2.data.condition.humidity
    ? data2.data.condition.humidity + "%"
    : "—";
  weather.windLevel = data2.data.condition.windLevel
    ? data2.data.condition.windLevel + "级"
    : "无风";
  return weather;
}

export function formatterProvincialStationRealtime(data) {
  if (!data.success) {
    return [];
  }
  let array1 = [].concat.apply([], data.data);
  let array2 = array1.filter(item => {
    if (item.station.indexOf("*") !== -1) {
      item.station = item.station.replace("(*)", "");
      return item;
    }
  });
  return array2.sort(utils.compare(["aqi"], 0));
}

export function formatterStationHourlyStatistics(data) {
  if (data.code !== 0) {
    return [];
  }
  let array = [].concat.apply([], data.data);
  return array.sort(utils.compare(["hourly", "aqi"], 0));
}

export function formatterSummaryStatistics(data) {
  if (data.code !== 0) {
    return [];
  }
  let array = [].concat.apply([], data.data);
  array.forEach(item => {
    let wgs = gps.bd09ToWgs84(item.lat, item.lng);
    item.lng = wgs.lng;
    item.lat = wgs.lat;
    return item;
  });
  return array.sort(utils.compare(["hourly", "aqi"], 0));
}

export function formatterVelocity(data) {
  if (data.code !== 0) {
    return [];
  }
  return [].concat.apply([], data.data);
}

const formatterDatetimeForEcharts = (datetime, key) => {
  if (key === "hour") {
    let date = new Date(datetime);
    return date.getHours();
  } else if (key === "day") {
    let date = new Date(datetime);
    return date.getDate();
  } else if (key === "month") {
    let date = new Date(datetime);
    return date.getMonth() + 1;
  }
};

export function formatterAqiForEcharts(array, key) {
  let colorList = {
    "warn-color-1": "#43ce17",
    "warn-color-2": "#efdc31",
    "warn-color-3": "#fa0",
    "warn-color-4": "#ff401a",
    "warn-color-5": "#d20040",
    "warn-color-6": "#9c0a4e"
  };
  let xAxisData = [],
    seriesData = [],
    aqiColorList = [];
  array.forEach(item => {
    let time = formatterDatetimeForEcharts(item.statsTime, key);
    xAxisData.push(time);
    seriesData.push({
      value: Math.round(item.aqi),
      measurement: "",
      time: item.statsTime,
      key: "AQI"
    });
    aqiColorList.push(colorList[utils.validateAqi(item.aqi)]);
  });
  return {
    key: "aqi",
    id: "echarts-station-aqi",
    title: "最近24小时AQI变化趋势",
    color: "#666",
    xAxisData: xAxisData,
    seriesName: "AQI",
    seriesData: seriesData,
    colorList: aqiColorList
  };
}

export function formatterAirQualityParametersForEcharts(array, key) {
  let colorList = {
    "warn-color-1": "#43ce17",
    "warn-color-2": "#efdc31",
    "warn-color-3": "#fa0",
    "warn-color-4": "#ff401a",
    "warn-color-5": "#d20040",
    "warn-color-6": "#9c0a4e"
  };
  let xAxisData = [],
    coSeriesData = [],
    coColorList = [],
    no2SeriesData = [],
    no2ColorList = [],
    so2SeriesData = [],
    so2ColorList = [],
    o3SeriesData = [],
    o3ColorList = [],
    pm25SeriesData = [],
    pm25ColorList = [],
    pm10SeriesData = [],
    pm10ColorList = [],
    tempSeriesData = [],
    humiSeriesData = [],
    thColorList = [];
  if (array) {
    array.forEach(item => {
      let time = formatterDatetimeForEcharts(item.statsTime, key);
      xAxisData.push(time);
      coSeriesData.push({
        value: item.co,
        measurement: "mg/m³",
        time: item.statsTime,
        key: "CO"
      });
      coColorList.push(colorList[utils.validateCo(item.co)]);
      no2SeriesData.push({
        value: item.no2,
        measurement: "ug/m³",
        time: item.statsTime,
        key: "NO2"
      });
      no2ColorList.push(colorList[utils.validateNo2(item.no2)]);
      so2SeriesData.push({
        value: item.so2,
        measurement: "ug/m³",
        time: item.statsTime,
        key: "SO2"
      });
      so2ColorList.push(colorList[utils.validateSo2(item.so2)]);
      o3SeriesData.push({
        value: item.o3,
        measurement: "ug/m³",
        time: item.statsTime,
        key: "O3"
      });
      o3ColorList.push(colorList[utils.validateO3(item.o3)]);
      pm25SeriesData.push({
        value: item.pm25,
        measurement: "ug/m³",
        time: item.statsTime,
        key: "PM25"
      });
      pm25ColorList.push(colorList[utils.validatePm2_5(item.pm25)]);
      pm10SeriesData.push({
        value: item.pm10,
        measurement: "ug/m³",
        time: item.statsTime,
        key: "PM10"
      });
      pm10ColorList.push(colorList[utils.validatePm10(item.pm10)]);
      tempSeriesData.push({
        value: item.temperature,
        measurement: "℃",
        time: item.statsTime,
        key: "TEMPERATURE"
      });
      thColorList.push(colorList["warn-color-1"]);
      humiSeriesData.push({
        value: item.humidity,
        measurement: "％",
        time: item.statsTime,
        key: "HUMIDITY"
      });
    });
  }
  return [
    {
      key: "pm25",
      id: "echarts-station-pm25",
      title: "PM2.5（ug/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "PM2.5",
      seriesData: pm25SeriesData,
      colorList: pm25ColorList
    },
    {
      key: "pm10",
      id: "echarts-station-pm10",
      title: "PM10（ug/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "PM10",
      seriesData: pm10SeriesData,
      colorList: pm10ColorList
    },
    {
      key: "temp",
      id: "echarts-station-temp",
      title: "温度（℃）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "温度",
      seriesData: tempSeriesData,
      colorList: thColorList
    },
    {
      key: "humi",
      id: "echarts-station-humi",
      title: "湿度（％）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "湿度",
      seriesData: humiSeriesData,
      colorList: thColorList
    },
    {
      key: "co",
      id: "echarts-station-co",
      title: "CO（mg/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "CO",
      seriesData: coSeriesData,
      colorList: coColorList
    },
    {
      key: "no2",
      id: "echarts-station-no2",
      title: "NO2（ug/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "NO2",
      seriesData: no2SeriesData,
      colorList: no2ColorList
    },
    {
      key: "so2",
      id: "echarts-station-so2",
      title: "SO2（ug/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "SO2",
      seriesData: so2SeriesData,
      colorList: so2ColorList
    },
    {
      key: "o3",
      id: "echarts-station-o3",
      title: "O3（ug/m³）",
      color: "#FFF",
      xAxisData: xAxisData,
      seriesName: "O3",
      seriesData: o3SeriesData,
      colorList: o3ColorList
    }
  ];
}

export function formatterVehicleControlState(data) {
  let anionState = 1;
  let highPressurePumpState = 1;
  let fanState = 1;
  let array = [].concat.apply([], data);
  array.forEach(item => {
    if (item.equipment === "anion") {
      anionState = item.state;
    } else if (item.equipment === "highPressurePump") {
      highPressurePumpState = item.state;
    } else if (item.equipment === "fan") {
      fanState = item.state;
    }
  });
  return {
    anionState: anionState,
    highPressurePumpState: highPressurePumpState,
    fanState: fanState
  };
}
